// Define namespace

var <?php ns(); ?> = {};


/**
 *
 */

<?php ns(); ?>.init = function()
{
	$('[rel=<?php ns(); ?>]').live
	(
		'click',
		function()
		{
			<?php ns(); ?>.show
			(
				$(this).data('classOfIcon'),
				$(this).data('title'),
				$(this).data('message'),
				$(this).data('buttons'),
				$(this).data('closeOnEscape')
			);
		}
	);	
}	


/**
 *
 */

<?php ns(); ?>.close = function()
{
	// Get the modal window
	
	var modalWindow = $('#<?php ns(); ?>');
	

	// Remove all buttons

	modalWindow.find('.modal-footer').html('');


	// Close it

	modalWindow.modal('hide');
}


/**
 *
 */

<?php ns(); ?>.show = function(classOfIcon, title, message, buttons, closeOnEscape)
{
	// Make sure the modal window is closed

	<?php ns(); ?>.close();

	
	// Update the content of the modal window

	for (i in buttons)
	{
		// Get the button

		var button = buttons[i];

		
		// Create an anchor

		var anchor = $('<a></a>').appendTo('#<?php ns(); ?> .modal-footer');

		
		// Setup the anchor

		anchor.html(button['label']);
		anchor.addClass('btn');

		if (button['isDefault'] === true)
		{
			anchor.addClass('btn-primary');
		}

		anchor.attr('href', '#');


		// Setup the trigger

		if (button['isClose'] === true)
		{
			anchor.click
			(
				function()
				{
					<?php ns(); ?>.close();
				}
			);
		}
		else if (button['callback'] !== undefined)
		{
			var argumentsAsString = null;

			if 
			(
				button['callbackArguments'] !== undefined &&
				button['callbackArguments'] !== null &&
				button['callbackArguments'] !== ''
			)
			{
				argumentsAsString = JSON.stringify(button['callbackArguments']);
			}		

			var callbackAsString = button['callback'] + "(" + argumentsAsString + ")";

			anchor.click
			(
				function()
				{
					<?php ns(); ?>.close();
					eval(callbackAsString);
				}
			);
		}
		else if (button['url'] !== undefined)
		{
			anchor.attr('href', button['url']);
		}
	}
	
	$('#<?php ns(); ?>_title').html(title);
	$('#<?php ns(); ?>_message').html(message);


	// Show it!
	
	$('#<?php ns(); ?>').modal('show');
}
